package struts.action;

import global.GlobalConstants;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;

import bean.UserObject;

/**
 * @author ben
 * 
 */
public class BaseDispatchAction extends LoggingDispatchAction {

	@Autowired
	UserObject userObject;

	// token检查出错时的错误code
	protected static final String TOKEN_ERROR_KEY = "Z3101W0003";

	private static final ObjectMapper jacksonMapper = new ObjectMapper();

	protected LogInfo getLogInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response,
			String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {

		LogInfo logInfo = new LogInfo();

		logInfo.setUserId(userObject.getUserId());
		logInfo.setMethodName(super.getMethod(name).getName());
		logInfo.setFunctionId(StringUtils.stripEnd(clazz.getSimpleName().toUpperCase(), "ACTION"));
		logInfo.setSessionId(request.getSession().getId());

		return logInfo;

	}

	/**
	 * <p>
	 * token检查
	 * </p>
	 * 进行token检查,并生成新的token
	 * 
	 * @param request
	 * @throws IllegalPageOperationException
	 *             token检查出错的时候
	 */
	protected void validateToken(HttpServletRequest request) {
		validateToken(request, true);
	}

	/**
	 * <p>
	 * token检查
	 * </p>
	 * 
	 * @param request
	 * @param save
	 *            检查后是否生成新的token
	 * @throws IllegalPageOperationException
	 *             token检查出错的时候
	 */
	protected void validateToken(HttpServletRequest request, boolean save) {
		validateToken(request, save, true);
	}

	/**
	 * <p>
	 * token检查
	 * </p>
	 * 
	 * @param request
	 * @param save
	 *            检查后是否生成新的token
	 * @param reset
	 *            检查后对token进行重置
	 * @throws IllegalPageOperationException
	 *             token检查出错的时候
	 */
	protected void validateToken(HttpServletRequest request, boolean save, boolean reset) {
		if (isTokenValid(request, reset) == false) {
			throw new IllegalPageOperationException(TOKEN_ERROR_KEY);
		}
		if (save) {
			saveToken(request);
		}
	}

	/**
	 * Utility method to write modelObject into response with jackson.
	 * 
	 * @param modelObject
	 *            the Model object
	 * @param response
	 *            the resource response
	 * @throws IOException
	 * @throws JsonMappingException
	 * @throws JsonGenerationException
	 */
	protected void writeJacksonResponse(Object modelObject, HttpServletResponse response) throws JsonGenerationException,
			JsonMappingException, IOException {
		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		jacksonMapper.writeValue(response.getWriter(), modelObject);

	}

	protected void writeAjaxErrorResponse(String message, Object modelObject, HttpServletResponse response) throws JsonGenerationException,
			JsonMappingException, IOException {
		response.setStatus(GlobalConstants.AJAX_RESPONSE_ERROR_STATUS);
		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		if (modelObject != null) {
			jacksonMapper.writeValue(response.getWriter(), modelObject);
		} else {
			jacksonMapper.writeValue(response.getWriter(), message);
		}
	}

}
